var maintPlanMore;
require(['canGrid','highcharts'], function () {
    var loadLastAggExamReport,
    $mftTypeRankChart = $("#mftTypeRankChart"),
    $allMftTypeRankChart = $("#allMftTypeRankChart"),
    $resolveMftTypeRankChart = $("#resolveMftTypeRankChart"),
    $unResolveMftTypeRankChart = $("#unResolveMftTypeRankChart"),
    checkExam = $("#checkExam");
	var postData = {};
	//所有的故障占比
	$allMftTypeRankChart.click(function () {
	    postData.type = null;
	    $allMftTypeRankChart.addActiveClass('');
	    loadMftTypeRankChart();
	});
	//修复的故障占比
	$resolveMftTypeRankChart.click(function () {
	    $resolveMftTypeRankChart.addActiveClass('');
	    postData.type = 0;
	    loadMftTypeRankChart();
	});
	//未修复的故障占比
	$unResolveMftTypeRankChart.click(function () {
	    $unResolveMftTypeRankChart.addActiveClass('');
	    postData.type = 1;
	    loadMftTypeRankChart();
	});
	//再次体检
	checkExam.click(function () {
	    $.ajaxExt({
	        url: "/reportExaminReport/getSaveExaminReport.htm",
			loadMask: {target: $('.health-wrap-inner')}
	    }).done(function () {
	        loadLastAggExamReport(); //上次的汇总体检报告
	        loadMftTypeRankChart(); //车辆故障排行饼状图
	    });
	});
	//上次的汇总体检报告
	loadLastAggExamReport = function () {
	    $.ajaxExt({
	        url: "/reportExaminReport/getLastAggExaminReport.htm",
			loadMask: {target: $('.health-wrap-inner')}
	    }).done(function (data) {
	        postData = data[0];
	        $('#lastAggExamReport').setViewData(data[0]);
	        loadMftTypeRankChart(); //车辆故障排行饼状图
	    });
	};
	
	function loadMftTypeRankChart() {
	    $.ajaxExt({
	        url: "/reportExaminReport/getMftNumList.htm",
			loadMask: {target: $('.health-wrap-inner')},
	        data: postData
	    }).done(function (data) {
	        if (data) {
	            var series = [{
	                type: 'pie',
	                name: '车辆故障排行',
	                data: []
	            }];
	            $.each(data, function (index, item) {
	                series[0].data.push({name: item.ecuMftName, y: item.mftNum});
	            });
	            showMftTypeRankChart($mftTypeRankChart, series);
	        }
	    });
	}
	
	/* 图表的配置 */
	function showMftTypeRankChart(divId, series) {
	    divId.highcharts({
	        chart: {
	            plotBackgroundColor: null,
	            plotBorderWidth: null,
	            marginLeft: 0,
	            marginRight: 0,
                marginBottom:70,
	            plotShadow: false
	        },
	        title: {
	            text: ''
	        },
	        credits: {
	            enabled: false
	        },
	        plotOptions: {
	            pie: {
	                allowPointSelect: true,
	                cursor: 'pointer',
	                dataLabels: {
	                    enabled: true,
	                    inside: true,
	                    format: '<b>{point.name}</b>: {point.percentage:.2f}%',
	                    style: {
	                        color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
	                    }
	                },
	                distance: -30,
	                showInLegend: true
	            }
	        },
	        tooltip: {
	            pointFormat: '{series.name}: <b>{point.percentage:.2f}%</b>'
	        },
            legend: {
                maxHeight:44,
                symbolHeight:10
            },
	        series: series
	    });
	}
	
	//TODO 体检列表上，单独的体检按钮
	function doExamByCarId(carId) {
	    $.ajaxExt({
	        url: "/reportExaminReport/getSaveExaminReport.htm",
	        data: {carId: carId}
	    }).done(function () {
	        reloadExamGridData();
	    });
	}
	
	loadLastAggExamReport(); //上次的汇总体检报告
	
    var $maintePlanGridList = $('#maintePlanGrid');
    var $checkExam = $("#checkExam");

    //维修手册跳转
    $('#turnTo').on('click', function () {
        $.openTab('7192178ee7624749b3fe36db3ff17f86');
    });
    
    //详细信息跳转
    $('#goToDetails').on('click', function () {
        $.openTab('f8db8e980e614b0eb26e7f7d0c1de402');
    });

    //维保计划
    $maintePlanGridList.canGrid({
        url: "/reportCarMaintePlan/getMaintenancePlanList.htm",
        multiselect: false,
        autoLoad: true,
        colModel: [
            {name: "maintenId", label: 'maintenId', key: true, hidden: true},
            {name: "licPltNo", label: '车牌号', width: 80,
            	formatter:function(rowIndex, val, rowData){
            		if(val){
            			return "<a onClick='maintPlanMore()'>"+val+"</a>";
            		}
            	}},
            {name: "totalMile", label: '当前运行里程', width: 100,
            	formatter:function(rowIndex, val, rowData){
            		if(val){
            			return "<a onClick='maintPlanMore()'>"+val+"</a>";
            		}
                }},
            {name: "lastMaintenDate", label: '上次维保日期', width: 100,
                formatter:function(rowIndex, val, rowData){
                	if(val){
            			return "<a onClick='maintPlanMore()'>"+val+"</a>";
            		}
                }},
            {name: "lastMaintenMile", label: '上次维保里程', width: 105,
                formatter:function(rowIndex, val, rowData){
                	if(val){
            			return "<a onClick='maintPlanMore()'>"+val+"</a>";
            		}
                }},
            {name: "maintenCgy", label: '计划维保类别', width: 90,
                formatter:function(rowIndex, val, rowData){
                	if(val){
            			return "<a onClick='maintPlanMore()'>"+val+"</a>";
            		}
                }},
            {name: "planMile", label: '计划维保里程', width: 105,
                formatter:function(rowIndex, val, rowData){
                	if(val){
            			return "<a onClick='maintPlanMore()'>"+val+"</a>";
            		}
                   }},
            {name: "remainMile", label: '剩余里程', width: 105,
                formatter:function(rowIndex, val, rowData){
                	if(val){
            			return "<a onClick='maintPlanMore()'>"+val+"</a>";
            		}
                }},
            {name: "planMaintenDate", label: '计划维保日期', width: 100,
                formatter:function(rowIndex, val, rowData){
                	if(val){
            			return "<a onClick='maintPlanMore()'>"+val+"</a>";
            		}
                }},
            {name: "remainTime", label: '剩余天数', width: 80,
                formatter:function(rowIndex, val, rowData){
                	if(val){
            			return "<a onClick='maintPlanMore()'>"+val+"</a>";
            		}
                }},
            {name: "partsCode", label: '建议更换零部件', width: 250,
                formatter:function(rowIndex, val, rowData){
                	if(val){
            			return "<a onClick='maintPlanMore()'>"+val+"</a>";
            		}
                }}
        ]
    });

    var $examContain = $('#examContain');

    //循环获取用户监控权限填充到界面
    var $carMonitor = $('#car-monitor-wrap');
    $.each(window.parent.monitorMenu, function (index, id) {
        $carMonitor.find('li[data-menu=' + id + ']').removeClass();
    });

    //监控点击跳转到响应的界面
    $carMonitor.on('click', 'a', function () {
        var $parent = $(this).parent();
        if ($parent.hasClass('gray')) {
            return;
        }
        window.parent.openTab($parent.attr('data-menu'));
    })    
    
    maintPlanMore = function(){
		var isInside = $.inArray('7c3f52086ec347649fba50b59a84494b',window.parent.GLOBAL_PARAMS.FUNC_AUTH_ID);
		if(isInside > 0){
			var data = {};
			data.lineId = lineId;
			$.openTab('7c3f52086ec347649fba50b59a84494b', 'itemSearch', [data]);
		}
	}    
});